###################################################################################################
# Franchise Expansion and Legislative Representation in the Early United States ###################
# Roll Call Analysis ##############################################################################
# Stephen Ansolabehere, Jaclyn Kaslovsky, and Michael Olson #######################################
###################################################################################################

###################################################################################################
# description #####################################################################################
###################################################################################################

# this script produces all results using roll call data. 

###################################################################################################
# working directory, packages, and function #######################################################
###################################################################################################

  require("dplyr")
  require("ggplot2")
  require("stargazer")
  require("lfe")

  setwd("C:/Users/flyfi/Dropbox/Research/franchise_expansion/franchise_expansion_R&R/Replication Package")

###################################################################################################
# Data ############################################################################################
###################################################################################################

  nph <- read.csv("rc_data_foranalysis.csv",stringsAsFactors = FALSE)

###################################################################################################
# Summary Statistics ##############################################################################
###################################################################################################

  nph_tabdat <- nph[,c("nokken_poole_dim1","prop_repeal","tax_repeal"
                       ,"pct_slave","pct_urb","pct_urb25","logpop","delegation_size"
                       ,"white","citizen","percent_slave_dist","pop_per_sqmi","logdistpop")]
  
  nph_summary_sg <- stargazer(nph_tabdat,
                              covariate.labels=c("First-Dimension Nokken-Poole","No Property Requirement","No Tax Requirement","Percent Enslaved",
                                                 "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                                 "White Requirement","Citizen Requirement",
                                                 "Percent Enslaved (Dist.)","Population Density (Dist.)","ln(Population) (Dist.)"),
                              summary.stat = c("mean","median","sd","min","max","n"),digits=2,
                              title="U.S. House Roll Call Data Summary Statistics",
                              label="house_summary")
  
  cat(nph_summary_sg, sep = '\n', file = "nph_summary.tex")
  
  fileConn<-file("withinstatesd.tex")
  writeLines(format(round(mean(aggregate(nokken_poole_dim1~state,data=nph,FUN=sd)[,2],na.rm=T),2),format="d",big.mark=','), fileConn)
  close(fileConn)

###################################################################################################    
# Analysis ########################################################################################
###################################################################################################

# plot

  # find first year each state relaxes property-holding requirements
  
    min_years <- aggregate(year~state,data=nph[nph$prop_repeal==1,],FUN=min)
      min_years <- plyr::rename(min_years,replace=c("year"="min_year"))
      
    nph <- merge(nph,min_years,by="state",all.x=T)

    nph$runner <- nph$year-nph$min_year+1
    nph_plot <- nph[!is.na(nph$runner),]
    
    ggplot(nph_plot,aes(x=runner,y=nokken_poole_dim1))+
      geom_smooth(data=nph_plot[nph_plot$runner<=0 & nph_plot$runner>=-6,],colour="black",size=2)+
      geom_smooth(data=nph_plot[nph_plot$runner>0 & nph_plot$runner<=6,],colour="black",size=2)+
      geom_vline(xintercept=0,linetype=2)+
      xlab("Years to Adoption")+
      ylab("Nokken-Poole Score")+
      theme_minimal()
    
    ggsave("prop_repeal_plot.jpeg",width = 10, height = 4, units="in",dpi=800)
    
# individual level ################################################################################

# ind. level, dw-nominate, house #

# fixed effects
  
  nph_fe_nocov <- felm(nokken_poole_dim1~prop_repeal
                       |state+year|0|state
                       ,data=nph)
  
  nph_fe_cov1   <- felm(nokken_poole_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |state+year|0|state
                        ,data=nph)
  
  nph_fe_cov2   <- felm(nokken_poole_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        +tax_repeal+white+citizen  
                        |state+year|0|state
                        ,data=nph)

# linear state trends

nph_li_nocov <- felm(nokken_poole_dim1~prop_repeal
                     |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                     ,data=nph)

nph_li_cov1   <- felm(nokken_poole_dim1~prop_repeal
                      +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                      |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                      ,data=nph)

nph_li_cov2   <- felm(nokken_poole_dim1~prop_repeal
                      +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                      +tax_repeal+white+citizen  
                      |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                      ,data=nph)

# quadratic state trends

nph_qu_nocov <- felm(nokken_poole_dim1~prop_repeal
                     |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state
                     ,data=nph)

nph_qu_cov1   <- felm(nokken_poole_dim1~prop_repeal
                      +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                      |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state
                      ,data=nph)

nph_qu_cov2   <- felm(nokken_poole_dim1~prop_repeal
                      +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                      +tax_repeal+white+citizen  
                      |(as.numeric(index)+as.numeric(index2)):factor(state)+factor(state)+factor(year)|0|state
                      ,data=nph)

# report results

  nph_li_sg <- stargazer(nph_fe_nocov,nph_fe_cov1,nph_fe_cov2,
                         nph_qu_nocov,nph_qu_cov1,nph_qu_cov2,
                         add.lines=list(c("State Trend","Fixed","Fixed","Fixed","Quadratic","Quadratic","Quadratic")
                                        ),
                         notes.append = FALSE,notes.label = "",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f","adj.rsq"),
                         dep.var.labels = "Nokken-Poole Score",column.sep.width="0pt",
                         covariate.labels=c("No Property Requirement",
                                            "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement"),
                         notes=paste("\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(nph_fe_nocov$clustervar[[1]])),"states. 
                                     The unit of observation is the legislator-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         label="linear",
                         title="U.S. Representative Roll-Call Voting, Alternative Trends")
  
  cat(nph_li_sg, sep = '\n', file = "nph_li.tex")
  
  nph_fe_sg <- stargazer(nph_li_nocov,nph_li_cov1,nph_li_cov2,
                         notes.append = FALSE,notes.label = "",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f"),float.env = "table",
                         dep.var.labels = "Nokken-Poole Score",
                         covariate.labels=c("No Property Requirement",
                                            "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement"),
                         notes=paste("\\parbox[t]{0.95\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                         Standard errors in parentheses adjusted for clustering within",      length(unique(nph_li_cov1$clustervar[[1]])),"states. 
                                       The unit of observation is the legislator-congress. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         label="nph_fe",table.layout ="-d-t-as-n",
                         table.placement = "!ht",single.row = T,
                         title="Franchise Expansion and U.S. House Roll-Call Voting") 
  
  cat(nph_fe_sg, sep = '\n', file = "nph_fe.tex")
         
###################################################################################################
# Causal Validity Check ###########################################################################
###################################################################################################

# lags and leads #  

nph_lag1   <- felm(nokken_poole_dim1~prop_repeal
                 +(pr1+pr_1)
                 +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                 |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                 ,data=nph)

nph_lag2   <- felm(nokken_poole_dim1~prop_repeal
                 +(pr1+pr2+pr_1+pr_2)
                 +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                 |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                 ,data=nph)

nph_lag3   <- felm(nokken_poole_dim1~prop_repeal
                 +(pr1+pr2+pr3+pr_1+pr_2+pr_3)
                 +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                 |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                 ,data=nph)

 
laglead_h_sg <-  stargazer(nph_lag1,nph_lag2,nph_lag3,
                           add.lines=list(
                             c("No. Clusters",length(unique(nph_lag1$clustervar[[1]])),
                               length(unique(nph_lag2$clustervar[[1]])),
                               length(unique(nph_lag3$clustervar[[1]])))),
                           notes.append = FALSE,notes.label = "",
                           report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                           omit.stat = c("rsq", "ser","f"),float.env = "table",
                           dep.var.labels = "Nokken-Poole Score",single.row = TRUE,
                           covariate.labels=c("No Property Requirement",
                                              "No Prop. Req. $+1$","No Prop. Req. $+2$","No Prop. Req. $+3$",
                                              "No Prop. Req. $-1$","No Prop. Req. $-2$","No Prop. Req. $-3$",
                                              "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size"#,
                           ),
                           notes="\\parbox[t]{0.9\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                           Clustered standard errors
                           in parentheses. The unit of observation is the legislator-Congress.
                           $^{**}p<0.05$, $^*p<0.10$.}",
                           label="pt_rc",
                           title="Parallel Trends Test, U.S. Representative Roll Calls")

cat(laglead_h_sg, sep = '\n', file = "laglead_h.tex")

# ###################################################################################################
# # Sample Robustness ###############################################################################
# ###################################################################################################
# 
# state-dropper, house

states <- sort(unique(nph$state))
state_prop <- rep(NA,length(states))
state_prop_se <- state_prop

for(i in 1:length(states)){

  loop <- nph[nph$state!=states[i],]

  reg   <- felm(nokken_poole_dim1~prop_repeal
              +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
              |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
              ,data=loop)

  state_prop[i] <- coef(reg)["prop_repeal"]
  state_prop_se[i] <- sqrt(reg$clustervcv["prop_repeal","prop_repeal"])

}

plotdat <- as.data.frame(cbind(state_prop,
                               state_prop_se
                               ))
plotdat <- cbind(states,plotdat)

ggplot(data=plotdat,aes(x=states,y=state_prop))+
  geom_point()+
  geom_errorbar(ymin=state_prop-2*state_prop_se,ymax=state_prop+2*state_prop_se,
                width=0.5)+
  geom_errorbar(ymin=state_prop-1.65*state_prop_se,ymax=state_prop+1.65*state_prop_se,
                width=0.3)+
  geom_hline(yintercept = 0,colour="indianred",linetype=2,size=1)+
  ylim(min(state_prop-2*state_prop_se),max(state_prop+2*state_prop_se))+
  xlab("Dropped State")+
  ylab("Coefficient on 'Property Requirement Repeal'")+
  theme_minimal()

ggsave("state_dropper.jpeg",width = 8, height = 4, units="in",dpi=800)


# year-dropper, house

years <- sort(unique(nph$year))
years_prop <- rep(NA,length(years))
years_prop_se <- years_prop

for(i in 1:length(years)){

  loop <- nph[nph$year!=years[i],]

  reg   <- felm(nokken_poole_dim1~prop_repeal
              +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
              |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
              ,data=loop)

  years_prop[i] <- coef(reg)["prop_repeal"]
  years_prop_se[i] <- sqrt(reg$clustervcv["prop_repeal","prop_repeal"])

}

plotdat <- as.data.frame(cbind(years_prop,
                               years_prop_se
                               ))
plotdat <- cbind(years,plotdat)

ggplot(data=plotdat,aes(x=years,y=years_prop))+
  geom_point()+
  geom_errorbar(ymin=years_prop-2*years_prop_se,ymax=years_prop+2*years_prop_se,
                width=0.5)+
  geom_errorbar(ymin=years_prop-1.65*years_prop_se,ymax=years_prop+1.65*years_prop_se,
                width=0.3)+
  ylim(min(years_prop-2*years_prop_se),max(c(years_prop+2*years_prop_se,0)))+
  geom_hline(yintercept = 0,colour="indianred",linetype=2,size=1)+
  xlab("Dropped Year")+
  ylab("Coefficient on 'Property Requirement Repeal'")+
  theme_minimal()

ggsave("year_dropper.jpeg",width = 8, height = 4, units="in",dpi=800)


# original thirteen colonies
  
  colonies <- c("MA","NH","CT","NY","PA","VA","NJ","DE","MD","GA","SC","NC","RI")
  
  nph$colony <- ifelse(is.element(nph$state,colonies),1,0)
  
  nph_orig13_nocov <- felm(nokken_poole_dim1~prop_repeal
                       |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                       ,data=nph[nph$colony==1,])
  
  nph_orig13_cov1   <- felm(nokken_poole_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph[nph$colony==1,])
  
  nph_orig13_cov2   <- felm(nokken_poole_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        +tax_repeal+white+citizen  
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph[nph$colony==1,])
  
  nph_orig13_sg <- stargazer(nph_orig13_nocov,nph_orig13_cov1,nph_orig13_cov2,
                         notes.append = FALSE,notes.label = "",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f"),float.env = "table",
                         dep.var.labels = "Nokken-Poole Score",
                         covariate.labels=c("No Property Requirement",
                                            "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement"),
                         notes=paste("\\parbox[t]{0.665\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with linear state trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(nph_orig13_nocov$clustervar[[1]])),"states. 
                                     The unit of observation is the legislator-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         label="nph_orig13",
                         table.placement = "!ht",
                         title="Franchise Expansion and U.S. House Roll-Call Voting, Original 13 Colonies") 
  
  cat(nph_orig13_sg, sep = '\n', file = "nph_orig13.tex")
  
  
# dw-nominate fixed effects
  
  dwh_fe_nocov <- felm(nominate_dim1~prop_repeal
                       |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                       ,data=nph)
  
  dwh_fe_cov1   <- felm(nominate_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph)
  
  dwh_fe_cov2   <- felm(nominate_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        +tax_repeal+white+citizen  
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph)
  
  dwh_fe_sg <- stargazer(dwh_fe_nocov,dwh_fe_cov1,dwh_fe_cov2,
                         notes.append = FALSE,notes.label = "",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f"),float.env = "table",
                         dep.var.labels = "DW-NOMINATE Score",
                         covariate.labels=c("No Property Requirement",
                                            "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement"),
                         notes=paste("\\parbox[t]{0.665\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with linear state trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(dwh_fe_nocov$clustervar[[1]])),"states. 
                                     The unit of observation is the legislator-year. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         label="dwh_fe",
                         table.placement = "!ht",
                         title="U.S. House Roll-Call Voting: Alternative Measure") 
  
  cat(dwh_fe_sg, sep = '\n', file = "dwh_fe.tex")
  
###################################################################################################
# Coding VA and NY Differently ####################################################################
###################################################################################################
  
  nph$prop_repeal_alt1 <- nph$prop_repeal
  nph[(nph$state=="NY" & nph$year>1804 & nph$prop_repeal==0),"prop_repeal_alt1"] <- 0.5
  nph[(nph$state=="VA" & nph$year>1830 & nph$prop_repeal==0),"prop_repeal_alt1"] <- 0.5
  
  nph_fe_nocov_alt1 <- felm(nokken_poole_dim1~prop_repeal_alt1
                       |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                       ,data=nph)
  
  nph_fe_cov1_alt1   <- felm(nokken_poole_dim1~prop_repeal_alt1
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph)
  
  nph_fe_cov2_alt1  <- felm(nokken_poole_dim1~prop_repeal_alt1
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        +tax_repeal+white+citizen  
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph)
  
  nph$prop_repeal_alt1 <- nph$prop_repeal
  nph[(nph$state=="NY" & nph$year>1804 & nph$prop_repeal==0),"prop_repeal_alt1"] <- 1
  nph[(nph$state=="VA" & nph$year>1830 & nph$prop_repeal==0),"prop_repeal_alt1"] <- 1
  
  nph$alt_out <- nph$nokken_poole_dim1
  
  nph_fe_nocov_alt2 <- felm(alt_out~prop_repeal_alt1
                       |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                       ,data=nph)
  
  nph_fe_cov1_alt2   <- felm(alt_out~prop_repeal_alt1
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph)
  
  nph_fe_cov2_alt2   <- felm(alt_out~prop_repeal_alt1
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        +tax_repeal+white+citizen  
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph)
  
  nph_code_sg <- stargazer(nph_fe_nocov_alt1,nph_fe_cov1_alt1,nph_fe_cov2_alt1,
                         nph_fe_nocov_alt2,nph_fe_cov1_alt2,nph_fe_cov2_alt2,
                         notes.append = FALSE,notes.label = "",font.size = "footnotesize",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f"),float.env = "table",
                         dep.var.labels = c("Coded as `0.5'","Coded as `1'"),
                         dep.var.caption = "Nokken-Poole Score",
                       covariate.labels=c("No Property Requirement - Alternative Measures",
                                          "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                          "No Tax Requirement","White Requirement","Citizen Requirement"),
                         notes=paste("\\parbox[t]{1.02\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within", length(unique(nph_fe_cov1_alt1$clustervar[[1]])),"states. The unit of observation is the legislator-Congress. 
                       The left three models code New York and Virginia as ``0.5'' for the periods when they have somewhat relaxed property requirements; The right three code them as ``0'', 
                        for the same years. $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         label="nph_fe_alt",
                       table.placement = "!ht",
                         title="Franchise Expansion and U.S. House Roll-Call Voting: Alternative Coding for NY and VA") 
  
  cat(nph_code_sg, sep = '\n', file = "nph_fe_alt.tex")
  
  
###################################################################################################
# Within District and Within Legislator Results ###################################################
###################################################################################################  
  
  nph <- nph %>% arrange(icpsr,year)
  nph <- transform(nph, 
                       index_icpsr = ave(year, icpsr, 
                                   FUN = function(x) rank(x, ties.method = "first")))
  
  icp_fe_cov1   <- felm(nokken_poole_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |factor(icpsr)+factor(year)|0|state
                        ,data=nph)
  
  icp_li_cov1   <- felm(nokken_poole_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |as.numeric(index_icpsr):factor(icpsr)+factor(icpsr)+factor(year)|0|state
                        ,data=nph)

  nph$distid <- paste(nph$state,nph$district_code,nph$dist_life)
  nph$outcome2 <- nph$nokken_poole_dim1
  
  nph <- nph %>% arrange(distid,year)
  nph <- transform(nph, 
                   index_dist = ave(year, distid, 
                                     FUN = function(x) rank(x, ties.method = "first")))
  
  dis_fe_cov1   <- felm(outcome2~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |factor(distid)+factor(year)|0|state
                        ,data=nph)
  
  dis_li_cov1   <- felm(outcome2~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        |as.numeric(index_dist):factor(distid)+factor(distid)+factor(year)|0|state
                        ,data=nph)
  
  nph_altunit_sg <- stargazer(icp_fe_cov1,icp_li_cov1,
                              dis_fe_cov1,dis_li_cov1,
                         add.lines=list(c("Unit Trends","FE","Linear",
                                          "FE","Linear"
                                          )),
                         notes.append = FALSE,notes.label = "",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f"),float.env = "table",
                         dep.var.labels = c("Legislator Trends","District Trends"),
                         dep.var.caption = "Nokken-Poole Score",
                         covariate.labels=c("No Property Requirement",
                                            "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size"),
                         notes=paste("\\parbox[t]{0.7\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with unit trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within", length(unique(icp_li_cov1$clustervar[[1]])), "states. The unit of observation is the legislator-Congress. 
                                     $^{**}p<0.05$, $^*p<0.10$.}", sep= " "),
                         label="nph_legdist",
                         table.placement = "!ht",
                         title="Franchise Expansion and U.S. House Roll-Call Voting: Alternative Unit Trends") 
  
  cat(nph_altunit_sg, sep = '\n', file = "nph_legdist.tex")
  
# models with district-level covariates
  
  covar_li_nocov <- felm(nokken_poole_dim1~prop_repeal
                         +percent_slave_dist+pop_per_sqmi+logdistpop
                       |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                       ,data=nph)
  
  covar_li_cov1   <- felm(nokken_poole_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        +percent_slave_dist+pop_per_sqmi+logdistpop
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph)
  
  covar_li_cov2   <- felm(nokken_poole_dim1~prop_repeal
                        +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                        +tax_repeal+white+citizen  
                        +percent_slave_dist+pop_per_sqmi+logdistpop
                        |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state
                        ,data=nph)
  
  nph_covar_sg <- stargazer(covar_li_nocov,covar_li_cov1,covar_li_cov2,
                         notes.append = FALSE,notes.label = "",
                         report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                         omit.stat = c("rsq", "ser","f"),float.env = "table",
                         dep.var.labels = "Nokken-Poole Score",
                         covariate.labels=c("No Property Requirement",
                                            "Percent Enslaved","Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size",
                                            "No Tax Requirement","White Requirement","Citizen Requirement",
                                            "Percent Enslaved (Dist.)","Pop. Density (Dist.)","ln(Population) (Dist.)"),
                         notes=paste("\\parbox[t]{0.7\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",      length(unique(covar_li_nocov$clustervar[[1]])),"states. 
                                     The unit of observation is the legislator-congress. Population Density measured in 10,000s of people per square mile.
                                     $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                         label="nph_covar",
                         table.placement = "!ht",
                         title="U.S. House Roll-Call Voting: District-Level Covariates") 
  
  cat(nph_covar_sg, sep = '\n', file = "nph_covar.tex")
  